import { tt } from "../ttapi_interface/ttapi.js";
import * as tt2 from "../ttapi_layer2/ttlayer2.js";
import { MainUI_FileTab } from "./MainUI_FileTab.js";
import { MainUI_Menu } from "./MainUI_Menu.js";
import * as EUI from "./eui/EUI.js"



import { EditorManager, IFileEditor, IEditorUI } from "./fileeditor/editor.js";

export class EditorFile {
    editor: IEditorUI;
    ui: tt2.QUI_IElement
    canopen: boolean = false;
    info: string = "";
    baritem: EUI.EUI_TabItem;
    Clone(): EditorFile {
        let file = new EditorFile();
        file.editor = this.editor;
        file.ui = this.ui;
        file.canopen = this.canopen;
        file.info = this.info;
        file.baritem = this.baritem;
        return file;
    }
    Clear() {
        this.editor = null;
        this.ui = null;
        this.baritem = null;
        this.canopen = false;
        this.info = "";
    }
}
export class MainUI {
    canvas: tt2.QUI_Canvas;
    font: tt.Font

    window: EUI.EUI_Window;
    menu: EUI.EUI_MenuBar;
    filetree: EUI.EUI_FileTree;


    uiFileTab: EUI.EUI_TabBar;
    uiEditArea: tt2.QUI_Container;
    btnEditor: tt2.QUI_Button;
    textEditor: tt2.QUI_Label;
    //临时打开的文件不进这里，只有进入编辑模式才能进
    alleditor: { [id: string]: EditorFile } = {};
    tempeditor: EditorFile = new EditorFile();//右侧活动的PreViewer
    Init(canvas: tt2.QUI_Canvas, font: tt.Font) {


        //用最简洁的方式暴力缩放，控制到和屏幕缩放值一样
        //一定要在app.Start之后
        let dradio = tt.graphic.getDevicePixelRadio();
        // tt.graphic.setMainScreenScale(dradio);
        canvas.scale = dradio * 2;


        this.canvas = canvas;
        this.font = font;

        this.canvas.target.ClearColor = tt.Color.Black;
        this.window = new EUI.EUI_Window(this.canvas);

        //总背景
        this._InitBack();


        //菜单
        MainUI_Menu._InitMenu(this);




    }
    OnUpdate(delta: number) {
        // if (this.priview != null)
        //     this.priview.OnUpdate(delta);
    }
    private _Priview_InitNull(filename: string) {
        let _ext = tt.PathTool.GetLongExt(tt.PathTool.GetFileName(tt.PathTool.CheckPath(filename)));
        if (this.uiEditArea.getChildCount() == 0) {

            let add = EUI.EUI_Style.CreateBackWithText("未知文件格式:" + _ext, tt.Color.White);

            this.uiEditArea.addChild(add);
            let label = this.uiEditArea.getChild(0).getChild(0) as tt2.QUI_Label;
            label.cut = true;
        }
        else {
            let label = this.uiEditArea.getChild(0).getChild(0) as tt2.QUI_Label;
            label.text = "未知文件格式:" + _ext;
        }

        this.tempeditor.Clear();
        this.tempeditor.info = "未知文件格式:" + _ext;
        this.uiFileTab.SelectItemAt(0);
        this.uiEditArea.removeChildAll();
    }
    _lockinchange: boolean = false;
    private async _ChangePrivewer(filename: string, editor: IFileEditor) {
        //加个锁，防止意料之外
        while (this._lockinchange) {
            await tt.sleep(10);
        }


        //更新filetab 的name
        this.uiFileTab.items[0].name = tt.PathTool.GetFileName(tt.PathTool.CheckPath(filename));
        this.uiFileTab.UpdateItemUIAt(0);

        //短路的情况
        if (this.tempeditor.editor == null && editor == null) {
            this._Priview_InitNull(filename);

            return;
        }
        if (this.tempeditor.editor != null) {
            if (this.tempeditor.editor.getFilename() == filename) {
                return;
            }
        }

        //上锁
        this._lockinchange = true;
        //清理
        if (this.tempeditor.editor != null) {
            await this.tempeditor.editor.OnExitAsync();
            this.tempeditor.editor = null;
        }

        this.uiEditArea.removeChildAll();



        //换新的
        if (editor != null) {
            let ui = new tt2.QUI_Container();
            ui.localRect.setAsFill();
            let _editor = editor.newEditorUI();
            let info = await _editor.OnInitAsync(filename, ui, this.window);
            if (info == null) {
                this.tempeditor.Clear();
                this.tempeditor.info = "遇到错误"
            }
            else {
                this.tempeditor.editor = _editor;
                this.tempeditor.ui = ui;
                this.tempeditor.canopen = info.canopen;
                this.tempeditor.info = info.text;

            }

            this.uiFileTab.SelectItemAt(0);
            this.uiEditArea.removeChildAll();
            this.uiEditArea.addChild(ui);


        }
        else {
            //this.tempeditor = null;
            this._Priview_InitNull(filename);

        }
        this._lockinchange = false;

    }

    private _InitBack() {
        let backpanel = EUI.EUI_Style.CreateDefaultPanel();

        backpanel.localRect.setHPosFill();
        backpanel.localRect.setVPosFill(20, 0);
        this.window.back.addChild(backpanel);

        let split = EUI.EUI_Style.CreateHSplit();

        backpanel.addChild(split);


        this.filetree = new EUI.EUI_FileTree();
        split.getPanel1().addChild(this.filetree);
        this.filetree.localRect.setAsFill();
        this.filetree.AddIcon(".jpg", EUI.EUI_Style.icon1);
        this.filetree.AddIcon(".jpeg", EUI.EUI_Style.icon1);
        this.filetree.AddIcon(".png", EUI.EUI_Style.icon1);
        this.filetree.AddIcon(".txt", EUI.EUI_Style.icon2);
        this.filetree.OnPick = (item: EUI.EUI_FileItem) => {
            console.warn("Pick a File:" + item.name);
            let eitem = this.alleditor[item.name]
            if (eitem != undefined) {
                // this.uiEditArea.removeChildAll();
                // this.uiEditArea.addChild(eitem.ui);
                this.uiFileTab.SelectItemAt(eitem.baritem.index)
            }
            else {
                let _ext = tt.PathTool.GetLongExt(tt.PathTool.GetFileName(tt.PathTool.CheckPath(item.name)));
                this._ChangePrivewer(item.name, EditorManager.GetEditor(_ext));
            }
        }

        MainUI_FileTab._InitFileTab(this,split.getPanel2());
    }
}